#include <gtk/gtk.h>
#include <libxfce4util/libxfce4util.h>
#include <libxfce4panel/xfce-panel-plugin.h>
#include <libxfce4panel/xfce-hvbox.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>

#include "ic.h"
#include "config.h"

typedef struct
{
	//widgets
    XfcePanelPlugin *plugin;
    GtkWidget *ebox;
	GtkWidget *label;
	GtkTooltips* tips;

	//runtime data
	int timer_id;

	config_t config;
} ICPlugin;

typedef struct 
{
	ICPlugin *timer;
	gboolean retval;
	report_t report;
	GError *error;
} UpdateData ;

static gboolean update_text(void *data)
{
	UpdateData* udata = data;
	char *msg = NULL;
	char *emsg = NULL;

	if(!udata->retval){
		msg = g_strdup("error");
		if(udata->error && udata->error->message){
			emsg = g_strdup(udata->error->message);
			g_error_free(udata->error);
		}
	}
	else{
		msg = g_strdup_printf("%0.2f руб.\n%0.2f mb\n%s", udata->report.balance, udata->report.traffic, udata->report.expired_on);
	}

	gtk_label_set_text(GTK_LABEL(udata->timer->label), msg);
	gtk_tooltips_set_tip(udata->timer->tips, udata->timer->ebox, emsg, NULL);

	g_free(emsg);
	g_free(msg);
	g_free(udata->report.expired_on);
	g_free(udata);

	return FALSE;
}

static void* get_report(gpointer data)
{
	ICPlugin *me = data;
	UpdateData *udata = g_new0(UpdateData, 1);

	udata->timer = me;
	udata->retval = ic_get_report(me->config.login, me->config.password, &udata->report, &udata->error);
	g_idle_add(update_text, udata);

	return NULL;
}

static gboolean poll(void* data)
{
	ICPlugin *me = data;

	gtk_label_set_text(GTK_LABEL(me->label), "Query...");

	static pthread_t thread;
	pthread_create(&thread, NULL, get_report, me);
	
	return TRUE;
}

gboolean install_timer(ICPlugin *me)
{
	if(me->timer_id){
		g_source_remove(me->timer_id);
		me->timer_id = 0;
	}

	poll(me);
	me->timer_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE, me->config.delay, poll, me, NULL);
	
	return FALSE;
}

void configure(XfcePanelPlugin *plugin, ICPlugin *me)
{
	config_run_dialog(&me->config, (ConfigCallback)install_timer, me);
}

ICPlugin *new (XfcePanelPlugin *plugin)
{
	ICPlugin *me;
	GtkWidget *box;

	/* allocate memory for the plugin structure */
	me = calloc (sizeof(*me), 1);

	/* pointer to plugin */
	me->plugin = plugin;

	/* create misc widgets */
	me->ebox = gtk_event_box_new();
	me->tips = gtk_tooltips_new();

	//init values
	config_load(&me->config, NULL);
	
	/* some plugin widgets */
	box = gtk_vbox_new(FALSE, 0);
	gtk_container_add (GTK_CONTAINER (me->ebox), box);
	me->label = gtk_label_new("Starting...");
	pango_layout_set_alignment(gtk_label_get_layout(GTK_LABEL(me->label)), PANGO_ALIGN_CENTER);
	gtk_misc_set_alignment(GTK_MISC(me->label), 0.5, 0.5);
	gtk_box_pack_start (GTK_BOX (box), me->label, TRUE, FALSE, 0);
	
	return me;
}

gboolean size_changed (XfcePanelPlugin *plugin, gint size, ICPlugin *me)
{
	GtkOrientation orientation;

	/* get the orientation of the plugin */
	orientation = xfce_panel_plugin_get_orientation (plugin);

	/* set the widget size */
	if (orientation == GTK_ORIENTATION_HORIZONTAL)
		gtk_widget_set_size_request (GTK_WIDGET (plugin), -1, size);
	else
		gtk_widget_set_size_request (GTK_WIDGET (plugin), size, -1);

	/* we handled the orientation */
	return TRUE;
}

static void destroy(XfcePanelPlugin *plugin, ICPlugin *me)
{
	g_object_unref(plugin);
}

void construct (XfcePanelPlugin *plugin)
{
	ICPlugin *me;

	//init threads
	if(!g_thread_supported())
		g_thread_init(NULL);

	// create the plugin
	me = new (plugin);

	// add the ebox to the panel
	gtk_container_add (GTK_CONTAINER (plugin), me->ebox);

	// show the panel's right-click menu on this ebox
	xfce_panel_plugin_add_action_widget (plugin, me->ebox);

	//configure-plugin
	g_signal_connect (G_OBJECT (plugin), "configure-plugin",
					  G_CALLBACK (configure), me);

	//free-data
	g_signal_connect (G_OBJECT (plugin), "free-data",
					  G_CALLBACK (destroy), me);

	//show the configure menu item and connect signal
	xfce_panel_plugin_menu_show_configure (plugin);
	g_signal_connect (G_OBJECT (plugin), "size-changed", G_CALLBACK (size_changed), me);

	//show widgets
	gtk_widget_show_all (me->ebox);

	g_idle_add((GSourceFunc)install_timer, me);
}

/* register the plugin */
XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL (construct);
